﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SourceGeneratorLibrary;
using SourceGeneratorSample.Services;

namespace SourceGeneratorSample
{
    internal class Program
    {
        public static async Task Main(string[] args)
        {
            var services = new ServiceCollection();// 创建ServiceCollection
            ConfigureServices(services);
            var serviceProvider = services.BuildServiceProvider(); // 创建ServiceProvider
            await serviceProvider.GetService<App>().Run(args); // app程序运行入口

            var serviceA = serviceProvider.GetRequiredService<ExampleServiceA>();
            serviceA.PrintMessage();
            var serviceC = serviceProvider.GetRequiredService<ExampleServiceC>();
            serviceC.PrintMessage();
            Console.ReadKey();
        }

        private static void ConfigureServices(IServiceCollection services)
        {
            services.AddLogging(builder =>
            {
                builder.AddConsole();
                builder.AddDebug();
            });// 配置日志

            var configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: false)
                .AddEnvironmentVariables()
                .Build(); // 创建 config

            services.AddServicesInSourceGeneratorSample();// 显试添加接口 自动注入服务
            services.AddAttributeServicesInSourceGeneratorSample();// 显试添加特性 自动注入服务

            services.AddTransient<App>();
        }
    }
}
